package lib_model.Transportion;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Transportion_bill implements ISql {

	public int id;
	public int factory_id_ref;
	public int warehouse_id_ref;
	public String transportion_type_name_ref;
	public String account_factory_username_ref;
	public String account_warehouse_username_get_ref;
	public Date date_create;

	public Transportion_bill() {
		// TODO Auto-generated constructor stub
	}
	
	

	@Override
	public String toString() {
		return "Transportion_bill [id=" + id + ", factory_id_ref="
				+ factory_id_ref + ", warehouse_id_ref=" + warehouse_id_ref
				+ ", transportion_type_name_ref=" + transportion_type_name_ref
				+ ", account_factory_username_ref="
				+ account_factory_username_ref
				+ ", account_warehouse_username_get_ref="
				+ account_warehouse_username_get_ref + ", date_create="
				+ date_create + "]";
	}



	@Override
	public ReturnObj Select() {
		String sql = "select * from dbo.transportion_bill";
		ReturnObj obj = new ReturnObj();
		ArrayList<Transportion_bill> arr = new ArrayList<Transportion_bill>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Transportion_bill a = new Transportion_bill();
				a.id = rs.getInt("id");
				a.factory_id_ref = rs.getInt("factory_id_ref");
				a.warehouse_id_ref = rs.getInt("warehouse_id_ref");
				a.transportion_type_name_ref = rs
						.getString("transportion_type_name_ref");
				a.account_factory_username_ref = rs
						.getString("account_factory_username_ref");
				a.account_warehouse_username_get_ref = rs
						.getString("account_warehouse_username_get_ref");
				a.date_create = rs.getDate("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		System.out.println(arr.size());
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.transportion_bill (factory_id_ref,   warehouse_id_ref,   transportion_type_name_ref,   account_factory_username_ref,   account_warehouse_username_get_ref,   date_create) values   ( ?,   ?,   ?,   ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		int key = 0;
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.factory_id_ref);
			preState.setInt(2, this.warehouse_id_ref);
			preState.setString(3, this.transportion_type_name_ref);
			preState.setString(4, this.account_factory_username_ref);
			preState.setString(5, this.account_warehouse_username_get_ref);
			obj.Result = (preState.executeUpdate() == 1)? true:false;
			ResultSet keys = preState.getGeneratedKeys();
			
			keys.next(); 
			key = keys.getInt(1); 
			keys.close();
			preState.close();
			obj.ObjResult = key;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.transportion_bill set factory_id_ref = ?,   warehouse_id_ref = ?,   transportion_type_name_ref = ?,   account_factory_username_ref = ?,   account_warehouse_username_get_ref = ?,   date_create = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.factory_id_ref);
			preState.setInt(2, this.warehouse_id_ref);
			preState.setString(3, this.transportion_type_name_ref);
			preState.setString(4, this.account_factory_username_ref);
			preState.setDate(5, (java.sql.Date) this.date_create);
			preState.setInt(6, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.transportion_bill where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, factory_id_ref, warehouse_id_ref, transportion_type_name_ref, account_factory_username_ref, account_warehouse_username_get_ref, date_create from dbo.transportion_bill where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Transportion_bill a = new Transportion_bill();
				a.id = rs.getInt("id");
				a.factory_id_ref = rs.getInt("products_id_ref");
				a.warehouse_id_ref = rs.getInt("transportion_billid_id_ref");
				a.transportion_type_name_ref = rs
						.getString("transportion_type_name_ref");
				a.account_factory_username_ref = rs
						.getString("account_factory_username_ref");
				a.account_warehouse_username_get_ref = rs
						.getString("account_warehouse_username_get_ref");
				a.date_create = rs.getDate("date_create");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}
}
